

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>packnet_sfm.utils.depth &mdash; PackNet-SfM 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../configs/configs.html">Configs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../scripts/scripts.html">Scripts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trainers/trainers.html">Trainers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../datasets/datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../networks/networks.html">Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../losses/losses.html">Losses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../loggers/loggers.html">Loggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../geometry/geometry.html">Geometry</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils/utils.html">Utils</a></li>
</ul>
<p class="caption"><span class="caption-text">Contact</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://tri.global">Toyota Research Institute</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/packnet-sfm">PackNet-SfM GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/DDAD">DDAD GitHub</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">PackNet-SfM</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>packnet_sfm.utils.depth</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for packnet_sfm.utils.depth</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Toyota Research Institute.  All rights reserved.</span>

<span class="kn">from</span> <span class="nn">matplotlib.cm</span> <span class="kn">import</span> <span class="n">get_cmap</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.image</span> <span class="kn">import</span> \
    <span class="n">gradient_x</span><span class="p">,</span> <span class="n">gradient_y</span><span class="p">,</span> <span class="n">flip_lr</span><span class="p">,</span> <span class="n">interpolate_image</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.types</span> <span class="kn">import</span> <span class="n">is_seq</span><span class="p">,</span> <span class="n">is_tensor</span>


<div class="viewcode-block" id="viz_inv_depth"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.viz_inv_depth">[docs]</a><span class="k">def</span> <span class="nf">viz_inv_depth</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">,</span> <span class="n">normalizer</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">percentile</span><span class="o">=</span><span class="mi">95</span><span class="p">,</span>
                  <span class="n">colormap</span><span class="o">=</span><span class="s1">&#39;plasma&#39;</span><span class="p">,</span> <span class="n">filter_zeros</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts an inverse depth map to a colormap for visualization.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    inv_depth : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth map to be converted</span>
<span class="sd">    normalizer : float</span>
<span class="sd">        Value for inverse depth map normalization</span>
<span class="sd">    percentile : float</span>
<span class="sd">        Percentile value for automatic normalization</span>
<span class="sd">    colormap : str</span>
<span class="sd">        Colormap to be used</span>
<span class="sd">    filter_zeros : bool</span>
<span class="sd">        If True, do not consider zero values during normalization</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    colormap : np.array [H,W,3]</span>
<span class="sd">        Colormap generated from the inverse depth map</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If a tensor is provided, convert to numpy</span>
    <span class="k">if</span> <span class="n">is_tensor</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">):</span>
        <span class="c1"># Squeeze if depth channel exists</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">inv_depth</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
            <span class="n">inv_depth</span> <span class="o">=</span> <span class="n">inv_depth</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">inv_depth</span> <span class="o">=</span> <span class="n">inv_depth</span><span class="o">.</span><span class="n">detach</span><span class="p">()</span><span class="o">.</span><span class="n">cpu</span><span class="p">()</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span>
    <span class="n">cm</span> <span class="o">=</span> <span class="n">get_cmap</span><span class="p">(</span><span class="n">colormap</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">normalizer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">normalizer</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">percentile</span><span class="p">(</span>
            <span class="n">inv_depth</span><span class="p">[</span><span class="n">inv_depth</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">filter_zeros</span> <span class="k">else</span> <span class="n">inv_depth</span><span class="p">,</span> <span class="n">percentile</span><span class="p">)</span>
    <span class="n">inv_depth</span> <span class="o">/=</span> <span class="p">(</span><span class="n">normalizer</span> <span class="o">+</span> <span class="mf">1e-6</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">cm</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">clip</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">))[:,</span> <span class="p">:,</span> <span class="p">:</span><span class="mi">3</span><span class="p">]</span></div>


<div class="viewcode-block" id="inv2depth"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.inv2depth">[docs]</a><span class="k">def</span> <span class="nf">inv2depth</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Invert an inverse depth map to produce a depth map</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    inv_depth : torch.Tensor or list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth map</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    depth : torch.Tensor or list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Depth map</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">is_seq</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">inv2depth</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">inv_depth</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="mf">1.</span> <span class="o">/</span> <span class="n">inv_depth</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mf">1e-6</span><span class="p">)</span></div>


<div class="viewcode-block" id="depth2inv"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.depth2inv">[docs]</a><span class="k">def</span> <span class="nf">depth2inv</span><span class="p">(</span><span class="n">depth</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Invert a depth map to produce an inverse depth map</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    depth : torch.Tensor or list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Depth map</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    inv_depth : torch.Tensor or list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth map</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">is_seq</span><span class="p">(</span><span class="n">depth</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">depth2inv</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">depth</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">inv_depth</span> <span class="o">=</span> <span class="mf">1.</span> <span class="o">/</span> <span class="n">depth</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mf">1e-6</span><span class="p">)</span>
        <span class="n">inv_depth</span><span class="p">[</span><span class="n">depth</span> <span class="o">&lt;=</span> <span class="mf">0.</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>
        <span class="k">return</span> <span class="n">inv_depth</span></div>


<div class="viewcode-block" id="inv_depths_normalize"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.inv_depths_normalize">[docs]</a><span class="k">def</span> <span class="nf">inv_depths_normalize</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Inverse depth normalization</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    inv_depths : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth maps</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    norm_inv_depths : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Normalized inverse depth maps</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">mean_inv_depths</span> <span class="o">=</span> <span class="p">[</span><span class="n">inv_depth</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span> <span class="k">for</span> <span class="n">inv_depth</span> <span class="ow">in</span> <span class="n">inv_depths</span><span class="p">]</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">inv_depth</span> <span class="o">/</span> <span class="n">mean_inv_depth</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mf">1e-6</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">inv_depth</span><span class="p">,</span> <span class="n">mean_inv_depth</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">,</span> <span class="n">mean_inv_depths</span><span class="p">)]</span></div>


<div class="viewcode-block" id="calc_smoothness"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.calc_smoothness">[docs]</a><span class="k">def</span> <span class="nf">calc_smoothness</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">,</span> <span class="n">images</span><span class="p">,</span> <span class="n">num_scales</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculate smoothness values for inverse depths</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    inv_depths : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth maps</span>
<span class="sd">    images : list of torch.Tensor [B,3,H,W]</span>
<span class="sd">        Inverse depth maps</span>
<span class="sd">    num_scales : int</span>
<span class="sd">        Number of scales considered</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    smoothness_x : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Smoothness values in direction x</span>
<span class="sd">    smoothness_y : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        Smoothness values in direction y</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">inv_depths_norm</span> <span class="o">=</span> <span class="n">inv_depths_normalize</span><span class="p">(</span><span class="n">inv_depths</span><span class="p">)</span>
    <span class="n">inv_depth_gradients_x</span> <span class="o">=</span> <span class="p">[</span><span class="n">gradient_x</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">inv_depths_norm</span><span class="p">]</span>
    <span class="n">inv_depth_gradients_y</span> <span class="o">=</span> <span class="p">[</span><span class="n">gradient_y</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">inv_depths_norm</span><span class="p">]</span>

    <span class="n">image_gradients_x</span> <span class="o">=</span> <span class="p">[</span><span class="n">gradient_x</span><span class="p">(</span><span class="n">image</span><span class="p">)</span> <span class="k">for</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">images</span><span class="p">]</span>
    <span class="n">image_gradients_y</span> <span class="o">=</span> <span class="p">[</span><span class="n">gradient_y</span><span class="p">(</span><span class="n">image</span><span class="p">)</span> <span class="k">for</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">images</span><span class="p">]</span>

    <span class="n">weights_x</span> <span class="o">=</span> <span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">torch</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">g</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">keepdim</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span> <span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">image_gradients_x</span><span class="p">]</span>
    <span class="n">weights_y</span> <span class="o">=</span> <span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">torch</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">g</span><span class="p">),</span> <span class="mi">1</span><span class="p">,</span> <span class="n">keepdim</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span> <span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">image_gradients_y</span><span class="p">]</span>

    <span class="c1"># Note: Fix gradient addition</span>
    <span class="n">smoothness_x</span> <span class="o">=</span> <span class="p">[</span><span class="n">inv_depth_gradients_x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">weights_x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_scales</span><span class="p">)]</span>
    <span class="n">smoothness_y</span> <span class="o">=</span> <span class="p">[</span><span class="n">inv_depth_gradients_y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">weights_y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_scales</span><span class="p">)]</span>
    <span class="k">return</span> <span class="n">smoothness_x</span><span class="p">,</span> <span class="n">smoothness_y</span></div>


<div class="viewcode-block" id="fuse_inv_depth"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.fuse_inv_depth">[docs]</a><span class="k">def</span> <span class="nf">fuse_inv_depth</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">,</span> <span class="n">inv_depth_hat</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;mean&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Fuse inverse depth and flipped inverse depth maps</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    inv_depth : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth map</span>
<span class="sd">    inv_depth_hat : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Flipped inverse depth map produced from a flipped image</span>
<span class="sd">    method : str</span>
<span class="sd">        Method that will be used to fuse the inverse depth maps</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    fused_inv_depth : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Fused inverse depth map</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;mean&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">inv_depth</span> <span class="o">+</span> <span class="n">inv_depth_hat</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;max&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">,</span> <span class="n">inv_depth_hat</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">method</span> <span class="o">==</span> <span class="s1">&#39;min&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">,</span> <span class="n">inv_depth_hat</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown post-process method </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">method</span><span class="p">))</span></div>


<div class="viewcode-block" id="post_process_inv_depth"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.post_process_inv_depth">[docs]</a><span class="k">def</span> <span class="nf">post_process_inv_depth</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">,</span> <span class="n">inv_depth_flipped</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;mean&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Post-process an inverse and flipped inverse depth map</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    inv_depth : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth map</span>
<span class="sd">    inv_depth_flipped : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Inverse depth map produced from a flipped image</span>
<span class="sd">    method : str</span>
<span class="sd">        Method that will be used to fuse the inverse depth maps</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    inv_depth_pp : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Post-processed inverse depth map</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">W</span> <span class="o">=</span> <span class="n">inv_depth</span><span class="o">.</span><span class="n">shape</span>
    <span class="n">inv_depth_hat</span> <span class="o">=</span> <span class="n">flip_lr</span><span class="p">(</span><span class="n">inv_depth_flipped</span><span class="p">)</span>
    <span class="n">inv_depth_fused</span> <span class="o">=</span> <span class="n">fuse_inv_depth</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">,</span> <span class="n">inv_depth_hat</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="n">method</span><span class="p">)</span>
    <span class="n">xs</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="n">W</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">inv_depth</span><span class="o">.</span><span class="n">device</span><span class="p">,</span>
                        <span class="n">dtype</span><span class="o">=</span><span class="n">inv_depth</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">mask</span> <span class="o">=</span> <span class="mf">1.0</span> <span class="o">-</span> <span class="n">torch</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="mf">20.</span> <span class="o">*</span> <span class="p">(</span><span class="n">xs</span> <span class="o">-</span> <span class="mf">0.05</span><span class="p">),</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">1.</span><span class="p">)</span>
    <span class="n">mask_hat</span> <span class="o">=</span> <span class="n">flip_lr</span><span class="p">(</span><span class="n">mask</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">mask_hat</span> <span class="o">*</span> <span class="n">inv_depth</span> <span class="o">+</span> <span class="n">mask</span> <span class="o">*</span> <span class="n">inv_depth_hat</span> <span class="o">+</span> \
           <span class="p">(</span><span class="mf">1.0</span> <span class="o">-</span> <span class="n">mask</span> <span class="o">-</span> <span class="n">mask_hat</span><span class="p">)</span> <span class="o">*</span> <span class="n">inv_depth_fused</span></div>


<div class="viewcode-block" id="compute_depth_metrics"><a class="viewcode-back" href="../../../utils/utils.depth.html#packnet_sfm.utils.depth.compute_depth_metrics">[docs]</a><span class="k">def</span> <span class="nf">compute_depth_metrics</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">gt</span><span class="p">,</span> <span class="n">pred</span><span class="p">,</span> <span class="n">use_gt_scale</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Compute depth metrics from predicted and ground-truth depth maps</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Metrics parameters</span>
<span class="sd">    gt : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Ground-truth depth map</span>
<span class="sd">    pred : torch.Tensor [B,1,H,W]</span>
<span class="sd">        Predicted depth map</span>
<span class="sd">    use_gt_scale : bool</span>
<span class="sd">        True if ground-truth median-scaling is to be used</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    metrics : torch.Tensor [7]</span>
<span class="sd">        Depth metrics (abs_rel, sq_rel, rmse, rmse_log, a1, a2, a3)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">crop</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">crop</span> <span class="o">==</span> <span class="s1">&#39;garg&#39;</span>

    <span class="c1"># Initialize variables</span>
    <span class="n">batch_size</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">gt_height</span><span class="p">,</span> <span class="n">gt_width</span> <span class="o">=</span> <span class="n">gt</span><span class="o">.</span><span class="n">shape</span>
    <span class="n">abs_diff</span> <span class="o">=</span> <span class="n">abs_rel</span> <span class="o">=</span> <span class="n">sq_rel</span> <span class="o">=</span> <span class="n">rmse</span> <span class="o">=</span> <span class="n">rmse_log</span> <span class="o">=</span> <span class="n">a1</span> <span class="o">=</span> <span class="n">a2</span> <span class="o">=</span> <span class="n">a3</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="c1"># Interpolate predicted depth to ground-truth resolution</span>
    <span class="n">pred</span> <span class="o">=</span> <span class="n">interpolate_image</span><span class="p">(</span><span class="n">pred</span><span class="p">,</span> <span class="n">gt</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;bilinear&#39;</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="c1"># If using crop</span>
    <span class="k">if</span> <span class="n">crop</span><span class="p">:</span>
        <span class="n">crop_mask</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">gt</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:])</span><span class="o">.</span><span class="n">byte</span><span class="p">()</span><span class="o">.</span><span class="n">type_as</span><span class="p">(</span><span class="n">gt</span><span class="p">)</span>
        <span class="n">y1</span><span class="p">,</span> <span class="n">y2</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mf">0.40810811</span> <span class="o">*</span> <span class="n">gt_height</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="mf">0.99189189</span> <span class="o">*</span> <span class="n">gt_height</span><span class="p">)</span>
        <span class="n">x1</span><span class="p">,</span> <span class="n">x2</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mf">0.03594771</span> <span class="o">*</span> <span class="n">gt_width</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="mf">0.96405229</span> <span class="o">*</span> <span class="n">gt_width</span><span class="p">)</span>
        <span class="n">crop_mask</span><span class="p">[</span><span class="n">y1</span><span class="p">:</span><span class="n">y2</span><span class="p">,</span> <span class="n">x1</span><span class="p">:</span><span class="n">x2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="c1"># For each depth map</span>
    <span class="k">for</span> <span class="n">pred_i</span><span class="p">,</span> <span class="n">gt_i</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">pred</span><span class="p">,</span> <span class="n">gt</span><span class="p">):</span>
        <span class="n">gt_i</span><span class="p">,</span> <span class="n">pred_i</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">gt_i</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">pred_i</span><span class="p">)</span>
        <span class="c1"># Keep valid pixels (min/max depth and crop)</span>
        <span class="n">valid</span> <span class="o">=</span> <span class="p">(</span><span class="n">gt_i</span> <span class="o">&gt;</span> <span class="n">config</span><span class="o">.</span><span class="n">min_depth</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">gt_i</span> <span class="o">&lt;</span> <span class="n">config</span><span class="o">.</span><span class="n">max_depth</span><span class="p">)</span>
        <span class="n">valid</span> <span class="o">=</span> <span class="n">valid</span> <span class="o">&amp;</span> <span class="n">crop_mask</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span> <span class="k">if</span> <span class="n">crop</span> <span class="k">else</span> <span class="n">valid</span>
        <span class="c1"># Stop if there are no remaining valid pixels</span>
        <span class="k">if</span> <span class="n">valid</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="c1"># Keep only valid pixels</span>
        <span class="n">gt_i</span><span class="p">,</span> <span class="n">pred_i</span> <span class="o">=</span> <span class="n">gt_i</span><span class="p">[</span><span class="n">valid</span><span class="p">],</span> <span class="n">pred_i</span><span class="p">[</span><span class="n">valid</span><span class="p">]</span>
        <span class="c1"># Ground-truth median scaling if needed</span>
        <span class="k">if</span> <span class="n">use_gt_scale</span><span class="p">:</span>
            <span class="n">pred_i</span> <span class="o">=</span> <span class="n">pred_i</span> <span class="o">*</span> <span class="n">torch</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">gt_i</span><span class="p">)</span> <span class="o">/</span> <span class="n">torch</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">pred_i</span><span class="p">)</span>
        <span class="c1"># Clamp predicted depth values to min/max values</span>
        <span class="n">pred_i</span> <span class="o">=</span> <span class="n">pred_i</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">min_depth</span><span class="p">,</span> <span class="n">config</span><span class="o">.</span><span class="n">max_depth</span><span class="p">)</span>

        <span class="c1"># Calculate depth metrics</span>

        <span class="n">thresh</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">max</span><span class="p">((</span><span class="n">gt_i</span> <span class="o">/</span> <span class="n">pred_i</span><span class="p">),</span> <span class="p">(</span><span class="n">pred_i</span> <span class="o">/</span> <span class="n">gt_i</span><span class="p">))</span>
        <span class="n">a1</span> <span class="o">+=</span> <span class="p">(</span><span class="n">thresh</span> <span class="o">&lt;</span> <span class="mf">1.25</span>     <span class="p">)</span><span class="o">.</span><span class="n">float</span><span class="p">()</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
        <span class="n">a2</span> <span class="o">+=</span> <span class="p">(</span><span class="n">thresh</span> <span class="o">&lt;</span> <span class="mf">1.25</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">float</span><span class="p">()</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
        <span class="n">a3</span> <span class="o">+=</span> <span class="p">(</span><span class="n">thresh</span> <span class="o">&lt;</span> <span class="mf">1.25</span> <span class="o">**</span> <span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">float</span><span class="p">()</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>

        <span class="n">diff_i</span> <span class="o">=</span> <span class="n">gt_i</span> <span class="o">-</span> <span class="n">pred_i</span>
        <span class="n">abs_diff</span> <span class="o">+=</span> <span class="n">torch</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">diff_i</span><span class="p">))</span>
        <span class="n">abs_rel</span> <span class="o">+=</span> <span class="n">torch</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">diff_i</span><span class="p">)</span> <span class="o">/</span> <span class="n">gt_i</span><span class="p">)</span>
        <span class="n">sq_rel</span> <span class="o">+=</span> <span class="n">torch</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">diff_i</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">gt_i</span><span class="p">)</span>
        <span class="n">rmse</span> <span class="o">+=</span> <span class="n">torch</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">diff_i</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span>
        <span class="n">rmse_log</span> <span class="o">+=</span> <span class="n">torch</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">mean</span><span class="p">((</span><span class="n">torch</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">gt_i</span><span class="p">)</span> <span class="o">-</span>
                                           <span class="n">torch</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">pred_i</span><span class="p">))</span> <span class="o">**</span> <span class="mi">2</span><span class="p">))</span>
    <span class="c1"># Return average values for each metric</span>
    <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="n">metric</span> <span class="o">/</span> <span class="n">batch_size</span> <span class="k">for</span> <span class="n">metric</span> <span class="ow">in</span>
        <span class="p">[</span><span class="n">abs_rel</span><span class="p">,</span> <span class="n">sq_rel</span><span class="p">,</span> <span class="n">rmse</span><span class="p">,</span> <span class="n">rmse_log</span><span class="p">,</span> <span class="n">a1</span><span class="p">,</span> <span class="n">a2</span><span class="p">,</span> <span class="n">a3</span><span class="p">]])</span><span class="o">.</span><span class="n">type_as</span><span class="p">(</span><span class="n">gt</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Toyota Research Institute (TRI)

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>